package com.bjqh.edu.UI.Activity.TeacherPersonal;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bjqh.edu.Base.AppConstants;
import com.bjqh.edu.Bean.User;
import com.bjqh.edu.R;
import com.bjqh.edu.UI.Activity.Index.ScheduleActivity;
import com.bjqh.edu.UI.Activity.Login.LoginActivity;
import com.bjqh.edu.UI.Activity.StudentPersonal.MySettingActivity;
import com.bjqh.edu.UI.Activity.StudentPersonal.PersonalInformationActivity;
import com.bjqh.edu.Widget.CircleImageView;
import com.bumptech.glide.Glide;
import com.google.gson.Gson;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.cache.CacheMode;
import com.lzy.okgo.callback.StringCallback;

import butterknife.BindView;
import butterknife.ButterKnife;
import okhttp3.Call;
import okhttp3.Response;

public class TeacherIndexActivity extends AppCompatActivity {

    @BindView(R.id.cv)
    CircleImageView cv;
    @BindView(R.id.tv_floatName)
    TextView tvFloatName;
    @BindView(R.id.tv_my_phone)
    TextView tvMyPhone;
    @BindView(R.id.kbv)
    LinearLayout kbv;
    @BindView(R.id.ll_information)
    LinearLayout llInformation;
    @BindView(R.id.ll_plan)
    LinearLayout llPlan;

    @BindView(R.id.ll_kcb)
    LinearLayout llKcb;
    @BindView(R.id.ll_setup)
    LinearLayout llSetup;
    User user;
    User.UserBean userBean;
    @BindView(R.id.ll_ljsk)
    LinearLayout llLjsk;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_teacher_index);
        ButterKnife.bind(this);
        initView();
    }

    private void initView() {
        OkGo.post(AppConstants.BASE_URL + "/user/userInfor.shtml")
                .tag(this)
                .cacheKey("cacheKey")
                .cacheMode(CacheMode.DEFAULT)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(String s, Call call, Response response) {
                        Log.e("user", s);
                        Gson gson = new Gson();
                        user = gson.fromJson(s, User.class);
                        if(user!=null)
                        {
                        if (user.getMsg().equals("success")) {
                            userBean = user.getUser();
                            Glide.with(TeacherIndexActivity.this)
                                    .load(AppConstants.IMG_URL + userBean.getHeader()).error(R.mipmap.iv_err)
                                    .into(cv);
                            tvFloatName.setText(userBean.getNickname());
                            tvMyPhone.setText(userBean.getMobile());

                        }


                    }
                    }
                    @Override
                    public void onError(Call call, Response response, Exception e) {

                    }
                });
        llInformation.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //跳转到个人信息界面
                if (user != null) {
                    if (user.getMsg().equals("timeout")) {
                        AppConstants.gotoLogin(TeacherIndexActivity.this);
                    } else {
                        Intent intent = new Intent(TeacherIndexActivity.this, TeacherInformationActivity.class);
                        startActivity(intent);
                    }

                }
            }
        });
        llPlan.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (user != null) {
                    if (user.getMsg().equals("timeout")) {
                        Intent intent = new Intent();
                        intent.setClass(TeacherIndexActivity.this, LoginActivity.class);
                        //   intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);  //注意本行的FLAG设置
                        startActivity(intent);
                    } else {
                        //学业规划
                        Intent intent = new Intent(TeacherIndexActivity.this, TeacherPlannActivity.class);
                        startActivity(intent);
                    }
                }
            }
        });

/*
        llOrder.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //我的订单
                if (user!=null){
                    if(user.getMsg().equals("timeout")){
                        Intent intent = new Intent();
                        intent.setClass(TeacherIndexActivity.this, LoginActivity.class);
                        // intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);  //注意本行的FLAG设置
                        startActivity(intent);
                    }
                    else {
                        Intent intent = new Intent(TeacherIndexActivity.this, OrderActivity.class);
                        startActivity(intent);
                    }}
            }
        });
*/


        llSetup.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //我的设置
                if (user != null) {
                    if (user.getMsg().equals("timeout")) {
                        Intent intent = new Intent();
                        intent.setClass(TeacherIndexActivity.this, LoginActivity.class);
                        // intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);  //注意本行的FLAG设置
                        startActivity(intent);
                    } else {
                        Intent intent = new Intent(TeacherIndexActivity.this, MySettingActivity.class);
                        startActivity(intent);
                    }
                }
            }
        });

        llLjsk.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //我的订单
                if (user!=null){
                    if(user.getMsg().equals("timeout")){
                        Intent intent = new Intent();
                        intent.setClass(TeacherIndexActivity.this, LoginActivity.class);
                        // intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);  //注意本行的FLAG设置
                        startActivity(intent);
                    }
                    else {
                        Intent intent = new Intent(TeacherIndexActivity.this, CumulativeClassActivity.class);
                        startActivity(intent);
                    }}
            }
        });
        llKcb.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (user!=null){
                    if(user.getMsg().equals("timeout")){
                    AppConstants.gotoLogin(TeacherIndexActivity.this);
                    }
                    else {
                        Intent intent = new Intent(TeacherIndexActivity.this, ScheduleActivity.class);
                        startActivity(intent);
                    }}

            }
        });
        //点击头像修改个人信息
        cv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //跳转到个人信息界面
                if (user!=null){
                    if(user.getMsg().equals("timeout")){
                        AppConstants.gotoLogin(TeacherIndexActivity.this);
                    }
                    else {
                        Intent intent = new Intent(TeacherIndexActivity.this, TeacherInformationActivity.class);
                        startActivity(intent);
                    }

                }
            }
        });


    }

   /* @Override
    protected void onDestroy() {
        super.onDestroy();
        OkGo.post(AppConstants.BASE_URL + "/sys/exit.shtml")
                .tag(this)
                .cacheKey("cacheKey")
                .cacheMode(CacheMode.DEFAULT)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(String s, Call call, Response response) {
                        Log.e("退出登录 ",s);

                    }
                    @Override
                    public void onError(Call call, Response response, Exception e) {
                        super.onError(call, response, e);
                    }
                });
    }*/
}
